{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 灰度图处理与图片保存"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1. 读取BGR图(默认读取)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2 #opencv的缩写为cv2\n",
    "import matplotlib.pyplot as plt # matplotlib库用于绘图展示\n",
    "import numpy as np   # numpy数值计算工具包\n",
    "\n",
    "# 魔法指令，直接展示图，Jupyter notebook特有\n",
    "%matplotlib inline   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(414, 500, 3)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "img = cv2.imread('01_Picture/01_cat.jpg') \n",
    "img.shape # (h,w,c) c表示 3 通道，这个 3 通道被 opencv 读进来是 BGR 的先后顺序的 3 通道    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2. 读取灰度图(设置读取)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[153, 157, 162, ..., 174, 173, 172],\n",
       "       [119, 124, 129, ..., 173, 172, 171],\n",
       "       [120, 124, 130, ..., 172, 171, 170],\n",
       "       ...,\n",
       "       [187, 182, 167, ..., 202, 191, 170],\n",
       "       [165, 172, 164, ..., 185, 141, 122],\n",
       "       [179, 179, 146, ..., 197, 142, 141]], dtype=uint8)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "img_gray = cv2.imread('01_Picture/01_cat.jpg',cv2.IMREAD_GRAYSCALE)    \n",
    "#img_gray = cv2.imread('01_Picture/01_cat.jpg',cv2.IMREAD_COLOR)  # BGR图  \n",
    "img_gray  # 只有一个通道，同样是 uint8 类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "type(img_gray): <class 'numpy.ndarray'>\n",
      "img_gray.size:  207000\n",
      "img_gray.dtype: uint8\n",
      "img_gray.shape: (414, 500)\n"
     ]
    }
   ],
   "source": [
    "print('type(img_gray):',type(img_gray))\n",
    "print('img_gray.size: ',img_gray.size)  # 414 × 500 = 20700\n",
    "print('img_gray.dtype:',img_gray.dtype)\n",
    "print('img_gray.shape:',img_gray.shape) # 只有一个通道"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3. 显示图片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def cv_show(name,img):\n",
    "    cv2.imshow(name,img)\n",
    "    cv2.waitKey(0)\n",
    "    cv2.destroyAllWindows()\n",
    "    \n",
    "cv_show('image_cat_gray',img_gray)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 4. 保存图片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cv2.imwrite('01_Picture/02_cat_gray.jpg',img_gray) # 保存图片"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.6.3",
   "language": "python",
   "name": "python3.6.3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
